package writable;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class ReduceTest extends Reducer<Text,FlowBean, NullWritable,FlowBean> {
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        Iterator<FlowBean> ite = values.iterator();
        int upSum = 0;
        int downSum = 0;
        int totalSum = 0;
        while(ite.hasNext()){
            FlowBean flowBean = ite.next();
            upSum += flowBean.getUpFlow();
            downSum += flowBean.getDownFlow();
        }
        totalSum = upSum + downSum;
        FlowBean fb = new FlowBean();
        fb.setPhone(key.toString());
        fb.setUpFlow(upSum);
        fb.setDownFlow(downSum);
        fb.setSumFlow(totalSum);
        context.write(NullWritable.get(),fb);
    }
}
